// pages/around/around.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 0,  //当前所在页面的 index

    indicatorDots: true, //是否显示面板指示点

    autoplay: true, //是否自动切换

    interval: 3000, //自动切换时间间隔

    duration: 800, //滑动动画时长

    circular: true, //是否采用衔接滑动

    imgUrls: [

      'http://rcqj3s7lx.hn-bkt.clouddn.com/zd.webp',

      'http://rcqj3s7lx.hn-bkt.clouddn.com/mdd.gif',

      'http://rcqj3s7lx.hn-bkt.clouddn.com/wq.webp'

    ],

    iconList: [{
      icon: 'shop',
      color: 'red',
      badge: 120,
      name: '餐饮',
      amap:'050000'
    }, {
      icon: 'recordfill',
      color: 'orange',
      badge: 1,
      name: '风景',
      amap:'110000'
    }, {
      icon: 'read',
      color: 'yellow',
      badge: 0,
      name: '科教',
      amap:'140000'
    }, {
      icon: 'service',
      color: 'olive',
      badge: 22,
      name: '生活服务',
      amap:'070000'      
    }, {
      icon: 'safe',
      color: 'cyan',
      badge: 0,
      name: '医疗'
    }, {
      icon: 'newshotfill',
      color: 'blue',
      badge: 0,
      name: '购物'
    }],
    gridCol:3,
    geo_address:'',
    location:'',
    inputValue:'',
    around_data:''
  },
  //轮播图的切换事件

  swiperChange: function(e) {

    this.setData({

      swiperCurrent: e.detail.current

    })

  },

  //点击指示点切换

  chuangEvent: function(e) {

    this.setData({

      swiperCurrent: e.currentTarget.id

    })

  },

  //点击图片触发事件

  swipclick: function(e) {

    console.log(this.data.swiperCurrent);

    wx.switchTab({

      url: this.data.links[this.data.swiperCurrent]

    })

  },

  // 事件函数:跳转事件
  nav_detail: function (e) {
    console.log("这是around页面的点击事件数据：", e.currentTarget.dataset)
    var title = e.currentTarget.dataset.name
    var amap = e.currentTarget.dataset.amap
    var now_location = this.data.location
    wx.navigateTo({
      // 'path?key=value&key2=value2'
      url: '/pages/around_detail/around_detail' + '?' + 'detail_title=' + title + '&' + 'detail_location='+now_location + '&' +'detail_amap='+amap,
    })
  },
  bindKeyInput: function (e) {
    console.log(e.detail.value)
    this.setData({
      inputValue: e.detail.value
    })
  },
  /**
   * 监听用户：“输入事件”
   */
  search:function(e){
    var that =this
    console.log("用户输入信息：",e.currentTarget.dataset.input)
    var inputv = e.currentTarget.dataset.input
    wx.request({
      url: 'https://restapi.amap.com/v3/place/around?parameters ', //仅为示例，并非真实的接口地址
      data: {
        key: 'cb3ed45c0d994599dd5cf7034f4be2e2',
        location: this.data.location,
        keywords:String(inputv)
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log(res.data)
        that.setData({
          around_data:res.data
        })
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that=this
    // 1. 获取当前位置--经纬度
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        console.log("获取当前位置--经纬度:",res)
        that.setData({
          location:longitude+','+latitude
        })
        // 先位置，在逆地理编码
        // 2. 高德API获取结构化地址
        wx.request({
          url: 'https://restapi.amap.com/v3/geocode/regeo?parameters', //仅为示例，并非真实的接口地址
          data: {
            key: 'cb3ed45c0d994599dd5cf7034f4be2e2',
            location: longitude+','+latitude
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success(res) {
            console.log("高德API获取结构化地址:",res.data)
            var component = res.data.regeocode.addressComponent
            that.setData({
              geo_address:component.city+' '+component.district+' '+component.township
            })
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})